


// Mysql数据库的增删改查
// select
// updata set
// insert into
// delete from


// MongoDB数据库的增删改查
// find findOne
// updata updateOne updateMany
// insert insertOne insertMany save 
// delete deleteOne remove



// show collections



// insert insertOne insertMany save  新增


db.users.insert({ username: "lala", age: 35 })
db.users.insert({ username: "皓哥", age: 1.1 })
db.users.insert([{ username: "小明", age: 28 }, { username: "lili", age: 40 }])

db.users.insertOne({ username: "英子姐", age: 32 })


// 查询数据条数
db.users.count()

db.users.save([
    {
        username: '大大',
        age: 1000
    },
    {
        username: '小小',
        age: 1000
    },
])

db.users.save([
    {
        username: 'lili'
    },
    {
        username: '大大'
    }
])

// 修改数据
// update  updateOne updateMany
// db.users.updateMany(query(条件),filed(字段))

db.users.updateMany(
    {

    }, {
    $set: {
        age: 24
    }
}
)

db.users.updateMany({}, {
    $set: {
        sex: new Date()
    }
})

db.users.updateMany({
    username: 'lili'
}, {
    $set: {
        sex: 'nan'
    }
})

db.users.updateOne({
    username:'皓哥'
},{
    $inc:{
       age:1
    }
})


db.users.updateOne({
    username:'皓哥'
},{
    $inc:{
       age:-20
    },
    $set:{
        word:'喝奶粉'
    }
})


// 默认是false
// true 表示插入 找不到就插入 找到就修改

db.users.update({
    username:'小明'
},{
    $set:{
        age:66
    }
},true)

// 第二个参数true修改所有的满足条件的数据 updateMany
db.users.update({

},{
    $set:{
        msg:'hello 各位'
    }
},false,true)


// delete deleteOne remove 删除

// db.users.delete(query)

db.users.deleteOne({
    username:'lili'
})

db.users.deleteMany({
    username:'大大'
})

db.users.remove({})

db.users.drop()


// find findOne查询
//  db.users.find(qurey,field)
// 这里的1表示的是显示，0是隐藏  
db.users.find()
db.users.find({},{username:1})
db.users.find({},{username:1,age:1})
db.users.find({},{username:1,age:1,_id:0})

db.users.find({
    username:'小小'
})
db.users.findOne({
    username:'小小'
})



// 去重查询
// Select username,age from users where username='皓哥' and age=25
db.users.distinct('username')
db.users.distinct('time')
db.users.distinct('age')


// $gt great then>
// $gte great then equal >=
// $it less then <
// $lte less then equal <=
db.users.find({
    age:{
        $gt:5,
        $lte:24
    }
},{
    username:1,
    age:1,
    _id:0
})

db.users.find({
    age:{
        $gt:5,
    }
},{
    username:1,
    age:1,
    _id:0
}).sort({age:1})//sort表示排序 后面的1表示升序还可以是-1表示降序


// $or  或
db.users.find({
    $or:[
        {
            username:'皓哥'
        },{
            age:24
        }
    ]
},{
    username:1,
    age:1,
    _id:0
})


db.users.find({
    $or:[
        {
            username:'皓哥'
        },{
            age:{
                $gt:30
            }
        }
    ]
},{
    username:1,
    age:1,
    _id:0
})


db.users.find({
    username:/i/
},{username:1,_id:0})

db.users.find({
    username:/^小/
},{username:1,_id:0})

db.users.find({
    username:/a$/
},{username:1,_id:0})

db.users.find({
    username:new RegExp('l')
},{
    username:1,age:1,_id:0
})

// 电影的查询
db.movies.find({},{title:1,year:1,_id:0})
db.movies.find({},{title:1,genres:1,_id:0})
db.movies.find({},{title:1,"rating.average":1,_id:0})
db.movies.find({},{title:1,"rating.average":1,_id:0}).sort({"rating.average":-1})
db.movies.find({"rating.average":{$gte:9.4},year:'1994'},{year:1,title:1,_id:0,"rating.average":1})
db.movies.find({"rating.average":{$gte:9.4},year:'1994'},{year:1,title:1,_id:0,"casts.name":1})
db.movies.find({"rating.average":{$gte:9.4},year:'1994'},{year:1,title:1,_id:0,"rating.average":1}).sort({year:-1,"rating.average":1})



// 限制条数 limit
db.movies.find({},{year:1,title:1,_id:0}).limit(10)

// 跳过 skip
db.movies.find({},{year:1,title:1,_id:0}).skip(5)
db.movies.find({},{year:1,title:1,_id:0}).skip(5).limit(5)

// 长度 条数 count所有集合数据 size满足条件的集合数据
db.movies.find({},{year:1,title:1,_id:0}).size()
db.movies.find({},{year:1,title:1,_id:0}).count()
db.movies.find({},{year:1,title:1,_id:0}).skip(5).size()//满足条件的长度
db.movies.find().skip(5).count()


// 最值
db.movies.find({},{year:1,title:1,_id:0}).sort({year:1}).limit(1)//最值
db.movies.find({},{year:1,title:1,_id:0}).sort({year:-1}).limit(-1)//最值